/*
 * Buzzer.c
 *
 *  Created on: 30.01.2011
 *      Author: Tavin
 */

#include <stdint.h>

#include "board.h"
#include "PWM.h"
#include "gpio.h"
#include "Buzzer.h"
#include "system.h"

BuzzerSound_t buzzerSounds [BUZZER_SOUND_MAX];

BuzzerData_t buzzerData;

	avr32_pwm_channel_t pwm_channel = { .ccnt = 0 };

void Buzzer_init()
{
	buzzerData.durationCounter=10;




	gpio_enable_module_pin(BUZZER_PWM_PIN,BUZZER_PWM_FUNCTION);




    pwm_channel.CMR.calg = PWM_MODE_LEFT_ALIGNED;       // Channel mode.
    pwm_channel.CMR.cpol = PWM_POLARITY_HIGH;            // Channel polarity.
    pwm_channel.CMR.cpd = PWM_UPDATE_DUTY;              // Not used the first time.
    pwm_channel.CMR.cpre = AVR32_PWM_CPRE_MCK_DIV_32;  // Channel prescaler.
    pwm_channel.cdty = 600;   // Channel duty cycle, should be < CPRD.
    pwm_channel.cprd = 750;  // Channel period.
    pwm_channel.cupd = BUZZER_PWM_CHANNEL_ID;   // Channel update is not used here.

    pwm_channel_init(BUZZER_PWM_CHANNEL_ID, &pwm_channel); // Set channel configuration to channel 0.



	buzzerSounds[0].duration=10;

	buzzerSounds[0].frequency=750;
	buzzerSounds[0].frequencyDelta=0;

	buzzerSounds[0].pulseWidth=100;
	buzzerSounds[0].pulseWidthDelta=10;

	if (System_isBuzzerOn()==TRUE)
		{
		Buzzer_on();
		}
	else
		{
		Buzzer_off();
		}


}

/****************************************************************************************/
/*																						*/
/*	Function : Buzzer_on									Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.5.21			Status : Working   			*/
/*																						*/
/****************************************************************************************/

void Buzzer_on()
{
	pwm_start_channels (1<<BUZZER_PWM_CHANNEL_ID);
}


/****************************************************************************************/
/*																						*/
/*	Function : Buzzer_off									Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.5.21			Status : Working   			*/
/*																						*/
/****************************************************************************************/

void Buzzer_off()
{
	pwm_stop_channels(1<<BUZZER_PWM_CHANNEL_ID);
}



/****************************************************************************************/
/*																						*/
/*	Function : Buzzer_playSound								Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.5.21			Status : Working   			*/
/*																						*/
/****************************************************************************************/

void Buzzer_playSound(int soundNumber)
{
	 volatile avr32_pwm_t *pwm = &AVR32_PWM;

	buzzerData.durationCounter=buzzerSounds[soundNumber].duration;

	pwm->channel[BUZZER_PWM_CHANNEL_ID].cprd=buzzerSounds[soundNumber].frequency;
	pwm->channel[BUZZER_PWM_CHANNEL_ID].cdty=buzzerSounds[soundNumber].pulseWidth;

   // pwm_start_channels (1<<BUZZER_PWM_CHANNEL_ID);
}



/****************************************************************************************/
/*																						*/
/*	Function : Buzzer_do									Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.5.21			Status : Working   			*/
/*																						*/
/****************************************************************************************/

void Buzzer_do()
{
	 volatile avr32_pwm_t *pwm = &AVR32_PWM;

	 if (buzzerData.durationCounter>0)
	buzzerData.durationCounter--;

	pwm->channel[BUZZER_PWM_CHANNEL_ID].cdty=pwm->channel[BUZZER_PWM_CHANNEL_ID].cdty+10;
	//pwm->channel[BUZZER_PWM_CHANNEL_ID].cprd=pwm->channel[BUZZER_PWM_CHANNEL_ID].cprd;
	if (buzzerData.durationCounter==0)
		{
		pwm->channel[BUZZER_PWM_CHANNEL_ID].cdty=pwm->channel[BUZZER_PWM_CHANNEL_ID].cprd;
		}




}


/****************************************************************************************/
/*																						*/
/*	Function : Buzzer_setSound								Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.5.21			Status : Working   			*/
/*																						*/
/****************************************************************************************/

void Buzzer_setSound(uint8_t soundNumber,BuzzerSound_t * mySound)
{
if (soundNumber<BUZZER_SOUND_MAX)
	{

	buzzerSounds[soundNumber].duration=mySound->duration;

	buzzerSounds[soundNumber].frequency=mySound->frequency;
	buzzerSounds[soundNumber].frequencyDelta=mySound->frequencyDelta;

	buzzerSounds[soundNumber].pulseWidth=mySound->pulseWidth;
	buzzerSounds[soundNumber].pulseWidthDelta=mySound->pulseWidthDelta;

	}




}
